const http = require("../../utils/http");

// Login.js
Page({
  data: {
    showStudentLogin: true, // 默认显示学号登录
    showPopupWindow: false, // 默认不显示弹窗
    studentNumber: '', // 学号
    password: '' // 密码
  },

  // 切换登录类型
  toggleLoginType: function(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      showStudentLogin: type === 'student'
    });
  },

  // 显示弹窗
  showPopup() {
    this.setData({
      showPopupWindow: true
    });
  },

  // 隐藏弹窗
  hidePopup() {
    this.setData({
      showPopupWindow: false
    });
  },

  // 阻止弹窗内容区域关闭弹窗
  catchTap() {
    // Prevent the popup from closing when clicking inside the popup content
  },
  
  // 手机号
  phonelog:function(){
    wx.switchTab({
      url: '/pages/User/Home/Home',
    })
  },

  // 跳转到服务条款或隐私政策页面
  navigateToReading: function(e) {
    wx.navigateTo({
      url: '/pages/Reading/Reading'
    });
  },

  // 获取学号输入
  onStudentNumberInput: function(e) {
    this.setData({
      studentNumber: e.detail.value
    });
  },

  // 获取密码输入
  onPasswordInput: function(e) {
    this.setData({
      password: e.detail.value
    });
  },

// 登录验证
login: function() {
  const { studentNumber, password } = this.data;

  if (!studentNumber || !password) {
    wx.showToast({
      title: '学号和密码不能为空',
      icon: 'none'
    });
    return;
  }

  const userNumber = studentNumber;
  const userPassword  =password;
  http('/login',{userNumber,userPassword},'GET',{}).then(response=>{
    if(response.data.success){
      wx.showToast({
        title: '登录成功',
        icon: 'success'
      });

      // 存储用户信息
      wx.setStorageSync('userInfo', response.data.user);
      console.log("userInfo: " + wx.getStorageSync('userInfo').id);
      wx.switchTab({
        url: '/pages/User/Home/Home',
      });
    }
    else{
      wx.showToast({
        title: response.data.message,
        icon: 'none'
      });
    }
  })
}
});